﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Collections;

namespace Yuanlin.Business
{
    public class SpecialNewsListControl : YuanlinUserControl,
        IReplyConverter<SpecialNewsReply, GetListOperation>
    {
        protected ValuedDropDownList categoryDropDownList;
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        private EntityList specialNews;

        private int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);
        private int kind = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.Kind);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                SpecialNewsListRequest request = new SpecialNewsListRequest();
                request.CategoryType = categoryType;
                request.Kind = kind;
                request.PageIndex = pager.PageIndex;
                request.PageSize = pager.PageSize;
                requestBinders.Add(RequestHelper.BuildGetSpecialNewsListRequest(this, request));
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            AjaxManager.Register(this, "AjaxMethod");
            categoryDropDownList.SelectedIndexChanged += new EventHandler(categoryDropDownList_SelectedIndexChanged);
        }
        protected void categoryDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            SpecialNewsListRequest request = new SpecialNewsListRequest();
            request.CategoryId = Globals.ChangeType<int>(categoryDropDownList.SelectedValue);
            request.CategoryType = categoryType;
            request.Kind = kind;
            request.PageIndex = pager.PageIndex;
            request.PageSize = pager.PageSize;
            specialNews = BusinessManager.GetSpecialNewsList(RequestHelper.BuildGetSpecialNewsListRequest(request));

            BindData(true);
        }
        private void BindData(bool isReBindData)
        {
            if (categoryDropDownList != null && !isReBindData)
            {
                SortedList categorys = new SortedList();
                categorys.Add(0, "<全部>");
                categorys.Add(1, "种植");
                categorys.Add(2, "养殖");
                categorys.Add(3, "资材");

                categoryDropDownList.DataSource = categorys;
                categoryDropDownList.DataTextField = "value";
                categoryDropDownList.DataValueField = "key";
                categoryDropDownList.DataBind();
            }

            if (list == null)
            {
                return;
            }

            list.DataSource = specialNews;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(specialNews.TotalCount);
                currentPage.TotalRecords = specialNews.TotalCount;
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }
        public string GetCategoryName(int categoryId)
        {
            Hashtable categorys = new Hashtable();
            categorys.Add(1, "种植");
            categorys.Add(2, "养殖");
            categorys.Add(3, "资材");
            if (categorys.Contains(categoryId))
            {
                return categorys[categoryId].ToString();
            }
            return "&nbsp;";
        }

        #region Ajax Method

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public void DeleteSpecialNews(int entityId)
        {
            BusinessManager.DeleteSpecialNews(entityId);
        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public int DeleteSpecialNewss(string entityIds)
        {
            if (string.IsNullOrEmpty(entityIds))
            {
                return 0;
            }
            string[] entityIdArray = entityIds.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int entityId = 0;
            foreach (string stringEntityId in entityIdArray)
            {
                try
                {
                    entityId = Int32.Parse(stringEntityId);
                }
                catch
                {
                    continue;
                }
                if (entityId > 0)
                {
                    DeleteSpecialNews(entityId);                
                }
            }
            return 0;
        }

        #endregion

        #region IReplyConverter<SpecialNewsReply,GetListOperation> Members

        public void ConvertReply(SpecialNewsReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                specialNews = reply.EntityList;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    BindData(false);
                }
            }
        }

        #endregion
    }
}